// 获取应用实例
const app = getApp();

Component({
  data: {
    selected: 0,
    color: "#7A7E83",
    selectedColor: "#3cc51f",
    list: [{
      pagePath: "/pages/home/home",
      iconPath: "https://objectstorageapi.bja.sealos.run/a0qepsyj-001/miniprogram-cdn/home.png",
      selectedIconPath: "https://objectstorageapi.bja.sealos.run/a0qepsyj-001/miniprogram-cdn/home-active.png",
      text: "首页"
    }, {
      pagePath: "/pages/activity/activity",
      iconPath: "https://objectstorageapi.bja.sealos.run/a0qepsyj-001/miniprogram-cdn/activity.png",
      selectedIconPath: "https://objectstorageapi.bja.sealos.run/a0qepsyj-001/miniprogram-cdn/activity-active.png",
      text: "活动"
    }, {
      pagePath: "/pages/profile/profile",
      iconPath: "https://objectstorageapi.bja.sealos.run/a0qepsyj-001/miniprogram-cdn/profile.png",
      selectedIconPath: "https://objectstorageapi.bja.sealos.run/a0qepsyj-001/miniprogram-cdn/profile-active.png",
      text: "我的"
    }]
  },
  attached() {
    // 尝试更新TabBar图标
    try {
      if (app && app.getFileURL) {
        const iconKeys = {
          'home': 'home',
          'home-active': 'home-active',
          'activity': 'activity',
          'activity-active': 'activity-active',
          'profile': 'profile',
          'profile-active': 'profile-active'
        };
        
        const newList = this.data.list.map((item, index) => {
          // 从原始路径提取图标名称
          const origIconName = item.iconPath.split('/').pop().split('.')[0];
          const origSelectedIconName = item.selectedIconPath.split('/').pop().split('.')[0];
          
          // 使用现有URL，如果getFileURL可用则尝试获取更新的URL
          return {
            ...item,
            iconPath: app.getFileURL(iconKeys[origIconName]) || item.iconPath,
            selectedIconPath: app.getFileURL(iconKeys[origSelectedIconName]) || item.selectedIconPath
          };
        });
        
        this.setData({
          list: newList
        });
        
        console.log('TabBar图标已更新');
      }
    } catch (error) {
      console.error('更新TabBar图标失败:', error);
    }
  },
  methods: {
    switchTab(e) {
      const data = e.currentTarget.dataset;
      const url = data.path;
      
      wx.switchTab({
        url
      });
      
      this.setData({
        selected: data.index
      });
      
      // 更新全局变量
      if (app && app.globalData) {
        app.globalData.tabBarIndex = data.index;
      }
    }
  }
}); 